package com.zego.wawaji.ui.Fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;

/**
 * 应用内Fragment基类
 */
public class BaseFragment extends Fragment {

    public BaseFragment() {
        super();
    }

    private LayoutInflater inflater;
    private ViewGroup container;
    private View contentView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        this.container = container;
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 为标题栏预留出状态栏空间
     */
    private void setImmerseLayout() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                View view = contentView.findViewById(R.id.base_title_bg);
//                if (view != null)
//                    view.setPadding(0, App.statusBarHeight, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置内容组件
     */
    public View setContentView(int resourcesId) {
        contentView = inflater.inflate(resourcesId, container, false);
        setImmerseLayout();
        return contentView;
    }

    /**
     * 获取内容组件
     */
    public View getContentView() {
        return this.contentView;
    }

    /**
     * 查找组件
     */
    public View findViewById(int id) {
        return contentView.findViewById(id);
    }

    /**
     * 设置标题
     */
    public void setTitle(String txt) {
        TextView textView = (TextView) contentView.findViewById(R.id.base_title_title);
        textView.setVisibility(View.VISIBLE);
        textView.setText(txt);
    }

    /**
     * 设置标题右侧文字
     */
    public void setTitleRight(String txt, View.OnClickListener listener) {
        TextView textView = (TextView) contentView.findViewById(R.id.base_title_right);
        textView.setVisibility(View.VISIBLE);
        textView.setOnClickListener(listener);
        textView.setText(txt);
    }

    /**
     * 设置标题左侧图片
     */
    public ImageView setTitleLeftImg(int res, View.OnClickListener listener) {
        ImageView iv = (ImageView) contentView.findViewById(R.id.base_left_img);
        ImageView back = (ImageView) contentView.findViewById(R.id.base_back);
        iv.setBackgroundResource(res);
        iv.setVisibility(View.VISIBLE);
        back.setVisibility(View.GONE);
        iv.setOnClickListener(listener);
        return iv;
    }

    /**
     * 设置标题右侧图片
     */
    public ImageView setTitleRightImg(int res, View.OnClickListener listener) {
        ImageView iv = (ImageView) contentView.findViewById(R.id.base_title_right_img);
        iv.setBackgroundResource(res);
        iv.setVisibility(View.VISIBLE);
        iv.setOnClickListener(listener);
        return iv;
    }
}
